package com.dx.mes.service.special.service;

import com.dx.common.table.ppt.Ret_box;
import com.dx.common.table.ppt.Ret_box_xfr;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetBoxXfrDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.transcoder.util.*;
import com.dx.mes.facade.special.service.IFspwipbankService;
import com.dx.mes.facade.special.tx.Fspwipbank.FspwipbankI;
import com.dx.mes.facade.special.tx.Fspwipbank.FspwipbankIA;
import com.dx.mes.facade.special.tx.Fspwipbank.FspwipbankO;

import org.hibernate.LockMode;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fspwipbankService")
public class FspwipbankService implements IFspwipbankService{
	private LogUtils logUtils;
	private static String TX_NAME = T_FSPWIPBNK;

	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetBoxXfrDao retBoxXfrDao;
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspwipbankService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspwipbankService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		Session session = retPrdInfoDao.getCrSession();

		long rtnCode = _NORMAL;
		String outMsg = null;
		FspwipbankI inTrx = new FspwipbankI();
		FspwipbankO outTrx = new FspwipbankO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FspwipbankI.class);

			rtnCode = subMainProc2(inTrx, outTrx);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logUtils.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private long subMainProc2(FspwipbankI inTrx, FspwipbankO outTrx) {
		long lRc = _NORMAL;
		char actionType = inTrx.getAction_flg().toCharArray()[0];
		switch (actionType) {
			case 'O': {
				lRc = wipbankOut(inTrx, outTrx);
				break;
			}
			case 'I': {
				lRc = wipbankIn(inTrx, outTrx);
				break;
			}
			default: {
				outTrx.setRtn_mesg("Invalide type input: [" + actionType + "]");
				return E_FIPINQBOX_INVALID_INPUT;
			}
		}
		return lRc;
	}

	/**
	 * Wip bank in
	 * 
	 * @return
	 */
	private long wipbankIn(FspwipbankI inTrx, FspwipbankO outTrx) {
		String gszMsgBuf = "";
		String tool_id_fk = inTrx.getTool_id();
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		String gEvtSeqID = GUIDGenerator.javaGUID();
		List<FspwipbankIA> iaryList = inTrx.getIary();
		for (FspwipbankIA iary :iaryList) {
			Ret_prd_info ret_prd_info = retPrdInfoDao.get(iary.getPrd_seq_id());
			if (null != ret_prd_info) {
			    if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
				    gszMsgBuf = "屏体已经在仓库中，请勿重复操作！";
				    outTrx.setRtn_mesg(gszMsgBuf);
					return E_FPWIPBANKT_INVALIED_BOX_BNK_FLG;
			    }
				    ret_prd_info.setEvt_cate(EVT_WIP_BANK_IN);
				    ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
				    ret_prd_info.setEvt_timestamp(gEvtTimestamp);
				    ret_prd_info.setEvt_seq_id(gEvtSeqID);
				    ret_prd_info.setBnk_flg(BANK_IN);
				    ret_prd_info.setPv_tool_id_fk(ret_prd_info.getNx_tool_id_fk());
				    ret_prd_info.setNx_tool_id_fk(tool_id_fk);
					retPrdInfoDao.update(ret_prd_info);
			} else {
				gszMsgBuf = "屏体[" +iary.getPrd_seq_id() + "]信息不存在！！！";
				outTrx.setRtn_mesg(gszMsgBuf);
				return BOX_INVALIED;
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return _NORMAL;
	}

	/**
	 * Wip bank out
	 * 
	 * @return
	 */
	private long wipbankOut(FspwipbankI inTrx, FspwipbankO outTrx) {
		String gszMsgBuf = "";
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		String gEvtSeqID = GUIDGenerator.javaGUID();
		List<FspwipbankIA> iaryList = inTrx.getIary();
		for (FspwipbankIA iary :iaryList) {
			Ret_prd_info ret_prd_info = retPrdInfoDao.get(iary.getPrd_seq_id());
			if (null != ret_prd_info) {
				if (!BANK_IN.equals(ret_prd_info.getBnk_flg())) {
					gszMsgBuf = "屏体不在仓库中，无法转出操作！";
					outTrx.setRtn_mesg(gszMsgBuf);
					return E_FPWIPBANKT_INVALIED_BOX_BNK_FLG;
				}
				ret_prd_info.setEvt_cate(EVT_WIP_BANK_OUT);
				ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_info.setEvt_timestamp(gEvtTimestamp);
				ret_prd_info.setEvt_seq_id(gEvtSeqID);
				ret_prd_info.setBnk_flg(BANK_OUT);
				ret_prd_info.setNx_tool_id_fk(ret_prd_info.getPv_tool_id_fk());
				ret_prd_info.setPv_tool_id_fk(_SPACE);
				retPrdInfoDao.update(ret_prd_info);
			} else {
				gszMsgBuf = "屏体[" +iary.getPrd_seq_id() + "]信息不存在！！！";
				outTrx.setRtn_mesg(gszMsgBuf);
				return BOX_INVALIED;
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return _NORMAL;
	}
}
